package com.clh.service.impl;

import java.util.List;
import java.util.UUID;

import com.clh.common.PageInfo;
import com.clh.dao.CarDao;
import com.clh.dao.impl.CarDaoImpl;
import com.clh.entity.Car;
import com.clh.service.CarService;


public class CarServiceImpl implements CarService{
	private CarDao carDao = new CarDaoImpl();

	@Override
	public List<Car> getCarList() {
		List<Car> list = carDao.find();
		return list;
	}

	@Override
	public int deleteById(String carId) {
		
		return carDao.deleteById(carId);
	}

	@Override
	public List<Car> getCarListByCondition(Car car) {
		StringBuffer carCard = new StringBuffer();
		if(car.getCarCard() != null && !"".equals(car.getCarCard())) {
			carCard.append("%").append(car.getCarCard()).append("%");
			car.setCarCard(carCard.toString());
		}
		//调用业务层
		List<Car> list = carDao.findByCindition(car);
		return list;
	}

	@Override
	public PageInfo<Car> getCarListByPage(Car car, int pageNum, int pageSize) {
		StringBuffer carCard = new StringBuffer();
		if(car.getCarCard() != null && !"".equals(car.getCarCard())) {
			carCard.append("%").append(car.getCarCard()).append("%");
			car.setCarCard(carCard.toString());
			
		}
		//1.调用持久层获得分页数据
		List<Car> list = carDao.findByPage(car, pageNum, pageSize);
		//2.调用持久层获得分页前总记录数
		Integer total = carDao.countByCondition(car);
		//3.封装分页对象
		PageInfo<Car> pageInfo = new PageInfo<Car>(list, pageNum, pageSize, total);
		
				
		return pageInfo;
	}

	@Override
	public boolean addCar(Car car) {
		//1.设置UUID
		UUID uuid = UUID.randomUUID();
		String carId = uuid.toString().replace("-", "");
		car.setCarId(carId);
		return carDao.insert(car) > 0;
	}

	@Override
	public Car getCarById(String carId) {
		// TODO Auto-generated method stub
		return carDao.findById(carId);
	}

	@Override
	public boolean modifyCar(Car car) {
		// TODO Auto-generated method stub
		return carDao.update(car) > 0 ;
	}

	@Override
	public List<Car> getCarByPhone(String carPhone) {
		// TODO Auto-generated method stub
		return carDao.findByPhone(carPhone);
	}

}
